ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
freemaster_appcmd.c
Go to the documentation of this file.
00001 /******************************************************************************
00002 *
00003 * Freescale Semiconductor Inc.
00004 * (c) Copyright 2004-2011 Freescale Semiconductor
00005 * ALL RIGHTS RESERVED.
00006 *
00007 ****************************************************************************/
00019 #include "freemaster.h"
00020 #include "freemaster_private.h"
00021 #include "freemaster_protocol.h"
00022  
00023 #if FMSTR_USE_APPCMD && (!FMSTR_DISABLE) 
00024 
00025 /***********************************
00026 *  local variables 
00027 ***********************************/
00028 
00029 static FMSTR_APPCMD_CODE pcm_nAppCmd;                               /* app.cmd code (to application) */
00030 static FMSTR_APPCMD_DATA pcm_pAppCmdBuff[FMSTR_APPCMD_BUFF_SIZE];   /* app.cmd data buffer  */
00031 static FMSTR_SIZE        pcm_nAppCmdLen;                            /* app.cmd data length */
00032 
00033 static FMSTR_APPCMD_RESULT  pcm_nAppCmdResult;                      /* app.cmd result code (from application) */
00034 static FMSTR_SIZE8 pcm_nAppCmdResultDataLen;
00035 
00036 #if FMSTR_MAX_APPCMD_CALLS > 0
00037 static FMSTR_APPCMD_CODE pcm_pAppCmdCallId[FMSTR_MAX_APPCMD_CALLS]; /* registerred callback commands */
00038 static FMSTR_PAPPCMDFUNC pcm_pAppCmdCallFunc[FMSTR_MAX_APPCMD_CALLS];  /* registerred callback handlers */
00039 #endif
00040 
00041 /***********************************
00042 *  local functions
00043 ***********************************/
00044 
00045 static FMSTR_INDEX FMSTR_FindAppCmdCallIndex(FMSTR_APPCMD_CODE nAppcmdCode);
00046 
00047 /**************************************************************************/
00053 void FMSTR_InitAppCmds(void)
00054 {
00055 #if FMSTR_MAX_APPCMD_CALLS
00056     FMSTR_INDEX i;
00057     
00058     for(i=0; i<FMSTR_MAX_APPCMD_CALLS; i++)
00059     {
00060         pcm_pAppCmdCallId[i] = FMSTR_APPCMDRESULT_NOCMD;
00061         pcm_pAppCmdCallFunc[i] = NULL;
00062     }
00063 #endif      
00064 
00065     pcm_nAppCmd = FMSTR_APPCMDRESULT_NOCMD;
00066     pcm_nAppCmdResult = FMSTR_APPCMDRESULT_NOCMD;
00067 }
00068 
00069 /**************************************************************************/
00080 FMSTR_BPTR FMSTR_StoreAppCmd(FMSTR_BPTR pMessageIO)
00081 {
00082     FMSTR_BPTR pResponse = pMessageIO;
00083     FMSTR_U8 nArgsLen;
00084     FMSTR_U8 nCode;
00085 
00086     /* the previous command not yet processed */
00087     if(pcm_nAppCmd != FMSTR_APPCMDRESULT_NOCMD)
00088     {
00089         return FMSTR_ConstToBuffer8(pResponse, FMSTR_STC_SERVBUSY);
00090     }
00091 
00092     pMessageIO = FMSTR_SkipInBuffer(pMessageIO, 1U);
00093     pMessageIO = FMSTR_ValueFromBuffer8(&nArgsLen, pMessageIO);
00094     pMessageIO = FMSTR_ValueFromBuffer8(&nCode, pMessageIO);
00095 
00096     /* args len is datalen minus one */
00097     nArgsLen--;
00098         
00099     /* does the application command fit to buffer ? */
00100     if (nArgsLen > (FMSTR_SIZE8) FMSTR_APPCMD_BUFF_SIZE)
00101     {
00102         return FMSTR_ConstToBuffer8(pResponse, FMSTR_STC_INVBUFF);
00103     }
00104     
00105     /* store command data into dedicated buffer */
00106     pcm_nAppCmd = nCode;
00107     pcm_nAppCmdLen = nArgsLen;
00108     
00109     /* data copy */
00110     if(nArgsLen)
00111     {
00112         FMSTR_ADDR appCmdBuffAddr;
00113         FMSTR_ARR2ADDR(appCmdBuffAddr, pcm_pAppCmdBuff);
00114         
00115         /*lint -e{534} ignoring return value */
00116         FMSTR_CopyFromBuffer(appCmdBuffAddr, pMessageIO, (FMSTR_SIZE8) nArgsLen);
00117     }
00118 
00119     /* mark command as "running" (without any response data) */
00120     pcm_nAppCmdResult = FMSTR_APPCMDRESULT_RUNNING;
00121     pcm_nAppCmdResultDataLen = 0U;
00122 
00123     return FMSTR_ConstToBuffer8(pResponse, FMSTR_STS_OK);
00124 }
00125 
00126 /**************************************************************************/
00141 FMSTR_BPTR FMSTR_GetAppCmdStatus(FMSTR_BPTR pMessageIO)
00142 {
00143 #if FMSTR_MAX_APPCMD_CALLS
00144     FMSTR_PAPPCMDFUNC pFunc = NULL;
00145     FMSTR_INDEX nIndex;
00146     
00147     /* time to execute the command's callback */
00148     if((nIndex = FMSTR_FindAppCmdCallIndex(pcm_nAppCmd)) >= 0)
00149     {
00150         pFunc = pcm_pAppCmdCallFunc[nIndex];
00151     }
00152     
00153     /* valid callback function found? */
00154     if(pFunc)
00155     {
00156         /* do execute callback, return value is app.cmd result code */
00157         pcm_nAppCmdResult = pFunc(pcm_nAppCmd, pcm_pAppCmdBuff, pcm_nAppCmdLen);
00158 
00159         /* nothing more to do with this command (i.e. command acknowledged) */
00160         pcm_nAppCmd = FMSTR_APPCMDRESULT_NOCMD;
00161     }
00162 #endif
00163 
00164     pMessageIO = FMSTR_ConstToBuffer8(pMessageIO, FMSTR_STS_OK);
00165     return FMSTR_ValueToBuffer8(pMessageIO, (FMSTR_U8) pcm_nAppCmdResult);
00166 }
00167 
00168 /**************************************************************************/
00179 FMSTR_BPTR FMSTR_GetAppCmdRespData(FMSTR_BPTR pMessageIO)
00180 {
00181     FMSTR_BPTR pResponse = pMessageIO;
00182     FMSTR_U8 nDataLen;
00183     FMSTR_U8 nDataOffset;
00184 
00185     /* the previous command not yet processed */
00186     if(pcm_nAppCmd != FMSTR_APPCMDRESULT_NOCMD)
00187     {
00188         return FMSTR_ConstToBuffer8(pResponse, FMSTR_STC_SERVBUSY);
00189     }
00190 
00191     pMessageIO = FMSTR_SkipInBuffer(pMessageIO, 1U);
00192     pMessageIO = FMSTR_ValueFromBuffer8(&nDataLen, pMessageIO);
00193     pMessageIO = FMSTR_ValueFromBuffer8(&nDataOffset, pMessageIO);
00194 
00195     /* the response would not fit into comm buffer */
00196     if(nDataLen > (FMSTR_U16)FMSTR_COMM_BUFFER_SIZE)
00197     {
00198         return FMSTR_ConstToBuffer8(pResponse, FMSTR_STC_RSPBUFFOVF);
00199     }
00200     
00201     /* the data would be fetched outside the app.cmd response data */
00202     if((((FMSTR_U16)nDataOffset) + nDataLen) > (FMSTR_SIZE8)pcm_nAppCmdResultDataLen)
00203     {
00204         return FMSTR_ConstToBuffer8(pResponse, FMSTR_STC_INVSIZE);
00205     }
00206     
00207     pResponse = FMSTR_ConstToBuffer8(pResponse, FMSTR_STS_OK);
00208 
00209     /* copy to buffer */
00210     {    
00211         FMSTR_ADDR appCmdBuffAddr;
00212         FMSTR_ARR2ADDR(appCmdBuffAddr, pcm_pAppCmdBuff);
00213         pResponse = FMSTR_CopyToBuffer(pResponse, appCmdBuffAddr, (FMSTR_SIZE8)nDataLen);
00214     }
00215     
00216     return pResponse;
00217 }
00218 
00219 /**************************************************************************/
00229 static FMSTR_INDEX FMSTR_FindAppCmdCallIndex(FMSTR_APPCMD_CODE nAppcmdCode)
00230 {
00231 #if FMSTR_MAX_APPCMD_CALLS > 0
00232     FMSTR_INDEX i;
00233     
00234     for(i=0; i<FMSTR_MAX_APPCMD_CALLS; i++)
00235     {
00236         if(pcm_pAppCmdCallId[i] == nAppcmdCode)
00237         {
00238             return i;
00239         }
00240     }
00241 #else
00242     /*lint -esym(528, FMSTR_FindAppCmdCallIndex) this function is 
00243       not referenced when APPCMD_CALLS are not used */
00244     FMSTR_UNUSED(nAppcmdCode);
00245 #endif
00246         
00247     return -1;
00248 }
00249 
00250 /**************************************************************************/
00258 void FMSTR_AppCmdAck(FMSTR_APPCMD_RESULT nResultCode)
00259 {
00260     pcm_nAppCmdResult = nResultCode;
00261     pcm_nAppCmdResultDataLen = 0U;
00262     
00263     /* waiting for a new command to come */
00264     pcm_nAppCmd = FMSTR_APPCMDRESULT_NOCMD;
00265 }
00266 
00267 /**************************************************************************/
00276 void FMSTR_AppCmdSetResponseData(FMSTR_ADDR nResultDataAddr, FMSTR_SIZE nResultDataLen)
00277 {
00278     /* any data supplied by user? */
00279     if(nResultDataAddr)
00280     {
00281         /* response data length is trimmed if response data would not fit into buffer */
00282         pcm_nAppCmdResultDataLen = (FMSTR_SIZE8) nResultDataLen;
00283         if(pcm_nAppCmdResultDataLen > (FMSTR_SIZE8) FMSTR_APPCMD_BUFF_SIZE)
00284         {
00285             pcm_nAppCmdResultDataLen = (FMSTR_SIZE8) FMSTR_APPCMD_BUFF_SIZE;
00286         }
00287 
00288         if(pcm_nAppCmdResultDataLen > 0U)
00289         {
00290             FMSTR_ADDR appCmdBuffAddr;
00291             FMSTR_ARR2ADDR(appCmdBuffAddr, pcm_pAppCmdBuff);
00292             FMSTR_CopyMemory(appCmdBuffAddr, nResultDataAddr, pcm_nAppCmdResultDataLen);
00293         }
00294     }
00295     else
00296     {
00297         /* no data being returned at all (same effect as pure FMSTR_AppCmdAck) */
00298         pcm_nAppCmdResultDataLen = 0U;
00299     }
00300 }
00301 
00302 /**************************************************************************/
00312 FMSTR_APPCMD_CODE FMSTR_GetAppCmd(void)
00313 {
00314 #if FMSTR_MAX_APPCMD_CALLS
00315     /* the user can never see the callback-registerred commands */
00316     if(FMSTR_FindAppCmdCallIndex(pcm_nAppCmd) >= 0)
00317     {
00318         return FMSTR_APPCMDRESULT_NOCMD;
00319     }
00320 #endif
00321 
00322     /* otherwise, return the appcomand pending */
00323     return pcm_nAppCmd;     
00324 }
00325     
00326 /**************************************************************************/
00336 FMSTR_APPCMD_PDATA FMSTR_GetAppCmdData(FMSTR_SIZE* pDataLen)
00337 {
00338     /* no command, no data */
00339     if(pcm_nAppCmd == FMSTR_APPCMDRESULT_NOCMD)
00340     {
00341         return NULL;
00342     }
00343     
00344 #if FMSTR_MAX_APPCMD_CALLS
00345     /* the user never sees the callback-registerred commands */
00346     if(FMSTR_FindAppCmdCallIndex(pcm_nAppCmd) >= 0)
00347     {
00348         return NULL;
00349     }
00350 #endif
00351 
00352     /* caller want to know the data length */
00353     if(pDataLen)
00354     {
00355         *pDataLen = pcm_nAppCmdLen;
00356     }
00357     
00358     /* data are saved in out buffer */
00359     return pcm_nAppCmdLen ? pcm_pAppCmdBuff : (FMSTR_APPCMD_PDATA) NULL;
00360 }
00361 
00362 /**************************************************************************/
00373 FMSTR_BOOL FMSTR_RegisterAppCmdCall(FMSTR_APPCMD_CODE nAppCmdCode, FMSTR_PAPPCMDFUNC pCallbackFunc)
00374 {
00375 #if FMSTR_MAX_APPCMD_CALLS > 0
00376 
00377     FMSTR_INDEX nIndex;
00378 
00379     /* keep "void" ID as reserved */
00380     if(nAppCmdCode == FMSTR_APPCMDRESULT_NOCMD)
00381     {
00382         return FMSTR_FALSE;
00383     }
00384     
00385     /* get index of app.cmd ID (if already set) */
00386     nIndex = FMSTR_FindAppCmdCallIndex(nAppCmdCode);
00387     
00388     /* when not found, get a free slot (only if registerring new callback) */
00389     if(nIndex < 0 && pCallbackFunc != NULL)
00390     {
00391         nIndex = FMSTR_FindAppCmdCallIndex(FMSTR_APPCMDRESULT_NOCMD);
00392     }
00393     
00394     /* failed? */
00395     if(nIndex < 0)
00396     {
00397         return FMSTR_FALSE;
00398     }
00399     
00400     /* register handler */
00401     pcm_pAppCmdCallFunc[nIndex] = pCallbackFunc;
00402     pcm_pAppCmdCallId[nIndex] = (FMSTR_APPCMD_CODE) (pCallbackFunc ? 
00403         nAppCmdCode : FMSTR_APPCMDRESULT_NOCMD);
00404         
00405     return FMSTR_TRUE;
00406     
00407 #else
00408     FMSTR_UNUSED(pCallbackFunc);
00409     FMSTR_UNUSED(nAppCmdCode);
00410     
00411     /* app.cmd callback not implemented */
00412     return FMSTR_FALSE;
00413         
00414 #endif  
00415 }
00416 
00417 #else /* FMSTR_USE_APPCMD && (!FMSTR_DISABLE) */
00418 
00419 /* void Application Command API functions */
00420 
00421 void FMSTR_AppCmdAck(FMSTR_APPCMD_RESULT nResultCode) 
00422 { 
00423     FMSTR_UNUSED(nResultCode);
00424 }
00425 
00426 void FMSTR_AppCmdSetResponseData(FMSTR_ADDR pResultData, FMSTR_SIZE nResultDataLen)
00427 {
00428     FMSTR_UNUSED(pResultData);
00429     FMSTR_UNUSED(nResultDataLen);
00430 }
00431 
00432 FMSTR_APPCMD_CODE FMSTR_GetAppCmd(void) 
00433 { 
00434     return FMSTR_APPCMDRESULT_NOCMD;
00435 }
00436 
00437 FMSTR_APPCMD_PDATA FMSTR_GetAppCmdData(FMSTR_SIZE* pDataLen) 
00438 { 
00439     FMSTR_UNUSED(pDataLen);
00440     return NULL; 
00441 }
00442 
00443 FMSTR_BOOL FMSTR_RegisterAppCmdCall(FMSTR_APPCMD_CODE nAppCmdCode, FMSTR_PAPPCMDFUNC pCallbackFunc) 
00444 { 
00445     FMSTR_UNUSED(nAppCmdCode);
00446     FMSTR_UNUSED(pCallbackFunc);
00447     return FMSTR_FALSE; 
00448 }
00449 
00450 /*lint -efile(766, freemaster_protocol.h) include file is not used in this case */
00451 
00452 #endif /* FMSTR_USE_APPCMD && (!FMSTR_DISABLE) */